"""
    Author:
    Date: 2019-05-30

    微信管理后台-资料审核
"""

import json
from django.shortcuts import render

from utils import ajax

from entities.consts import *
from entities import profile_mgr
from entities import user_mgr
from entities import vchat_api_mgr
from utils.log import Log

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 照片审核首页 """
    context = helpers.build_context(request)
    context['waiting_auth_profile_count'] = profile_mgr.get_profile_count_by_auth_status(PHOTO_AUTH_STATUS_WAITING, request)
    context['unauthorized_profile_count'] = profile_mgr.get_profile_count_by_auth_status(PHOTO_AUTH_STATUS_REJECT, request)
    context['authorized_profile_count'] = profile_mgr.get_profile_count_by_auth_status(PHOTO_AUTH_STATUS_AGREE, request)
    return render(request, 'gma/profile/profile_home.html', context)


@gma_login_required
def waiting_profiles(request):
    """ 待审的主播资料列表 """
    context = helpers.build_context(request)
    context['profiles'] = profile_mgr.get_profile_list_by_auth_status(PROFILE_AUTH_STATUS_WAITING, 100, request)
    return render(request, 'gma/profile/profile_list.html', context)


@gma_login_required
def rejected_profiles(request):
    """ 审核被拒的主播资料 """
    context = helpers.build_context(request)
    context['profiles'] = profile_mgr.get_profile_list_by_auth_status(PHOTO_AUTH_STATUS_REJECT, 100, request)
    return render(request, 'gma/profile/profile_list.html', context)


@gma_login_required
def agreed_profiles(request):
    """ 审核通过的主播资料 """
    context = helpers.build_context(request)
    context['profiles'] = profile_mgr.get_profile_list_by_auth_status(PHOTO_AUTH_STATUS_AGREE, 100, request)
    return render(request, 'gma/profile/profile_list.html', context)


@gma_login_required
def auth_profile(request, pid):
    """ 主播资料的审核页面 """
    profile = profile_mgr.get_profile_info(pid)

    context = helpers.build_context(request)
    context['user'] = user_mgr.get_user_detail(profile['uid'])
    context['profile'] = profile
    return render(request, 'gma/profile/auth_profile.html', context)


# @gma_login_required
# def search_profile(request):
#     """ 查找审核资料 """
#     search_value = request.GET.get('search_value', '')
#     if search_value == '' or not search_value.isdigit():
#         return helpers.show_page_500(request, '请输入主播的id!')
#
#     uid = int(search_value)
#     if uid < 120000:
#         uid += 120000
#
#     profile = profile_mgr.get_user_authing_profile_by(uid)
#     if profile is None:
#         return helpers.show_page_tips(request, '未找到该主播的图片审核记录!')
#
#     pid = profile['id']
#     return auth_profile(request, pid)


@gma_login_required
def api_agree_profile(request):
    """ 同意资料修改 """
    vid = request.POST['profile_id'].strip()
    profile = profile_mgr.get_profile_info(vid)
    if not profile:
        return ajax.error_response('无效的资料id!')

    # 更改审核状态
    success = profile_mgr.update_profile_auth_status(vid, PROFILE_AUTH_STATUS_AGREE)
    if not success:
        Log.error(f'Agree profile status failed: vid={vid}')
    else:
        Log.info(f'Agree profile status ok: vid={vid}')

    # 更新用户照片
    params = {
        'uid': int(profile['uid']),
    }
    result = vchat_api_mgr.update_user_profile(params)
    Log.info(f'Request api result: {result}')

    return ajax.success_response()


@gma_login_required
def api_reject_profile(request):
    """ 拒绝资料修改 """
    vid = request.POST['profile_id'].strip()
    profile = profile_mgr.get_profile_info(vid)
    if not profile:
        return ajax.error_response('无效的资料id!')

    success = profile_mgr.update_profile_auth_status(vid, PROFILE_AUTH_STATUS_REJECT)
    if not success:
        Log.error(f'Reject profile status failed: vid={vid}')
    else:
        Log.info(f'Reject profile status ok: vid={vid}')

    data = {}
    return ajax.success_response(data)


if __name__ == '__main__':
    print('ok')
